<?php
/**
 * Clase Base FN para BaseApFacturaLineaFN
 *
 * @package    
 * @subpackage BaseApFacturaLineaFN
 * @author     hans
 * @version    SVN:: BaseApFacturaLineaFN.class.php,v 1.1 2012-10-07 03:37:37 Hans.parra Exp $
 */

require_once($_SERVER['DOCUMENT_ROOT'].'/gourmet-express/trunk/src/class/po/facturas/BN/ApFacturaLineaBN.class.php');

class BaseApFacturaLineaFN extends ApFacturaLineaBN
{
  private static $fieldNames = array (
    'FacId'=>'facpl_facId', 'LineaId'=>'facpl_lineaId', 'LineaTipo'=>'facpl_lineaTipo', 'ArtId'=>'facpl_artId', 'ArtNombre'=>'facpl_artNombre', 'ImpuestoAfectoSw'=>'facpl_impuestoAfecto_sw', 'Cant'=>'facpl_cant', 'UoM'=>'facpl_UoM', 'Precio'=>'facpl_precio', 'PrecioMn'=>'facpl_precio_mn', 'Impuesto'=>'facpl_impuesto', 'Dcto'=>'facpl_dcto', 'Subtotal'=>'facpl_subtotal', 'SubtotalNeto'=>'facpl_subtotalNeto', 'Obs'=>'facpl_obs', 'CreaUsr'=>'facpl_creaUsr', 'CreaFecha'=>'facpl_creaFecha', 'ModifUsr'=>'facpl_modifUsr', 'ModifFecha'=>'facpl_modifFecha'  
  );

  public function select()
  {
    try
    {
      /*$tamPag = !property_exists($this, 'tamPag')?9999:$this->tamPag;
      $pagina = !property_exists($this, 'page')||$this->page==""?1:$this->page;
      $numeroRegistros = $this->selectNumRows();
      $limitInf = ($pagina-1)*$tamPag;*/

      $where = $this->buildConsult();
      //$orden = $this->buildOrderByField();

      $o_conexion = new BaseDatos();
      $con = $o_conexion->conectar();
      //$sql = "SELECT facpl_facId, facpl_lineaId, facpl_lineaTipo, facpl_artId, facpl_artNombre, facpl_impuestoAfecto_sw, facpl_cant, facpl_UoM, facpl_precio, facpl_precio_mn, facpl_impuesto, facpl_dcto, facpl_subtotal, facpl_subtotalNeto, facpl_obs, facpl_creaUsr, facpl_creaFecha, facpl_modifUsr, facpl_modifFecha FROM ap_factura_linea WHERE 1 ".$where." ".$orden."  LIMIT ".$limitInf.",".$tamPag;
      $sql = "SELECT facpl_facId, facpl_lineaId, facpl_lineaTipo, facpl_artId, facpl_artNombre, facpl_impuestoAfecto_sw, facpl_cant, facpl_UoM, facpl_precio, facpl_precio_mn, facpl_impuesto, facpl_dcto, facpl_subtotal, facpl_subtotalNeto, facpl_obs, facpl_creaUsr, facpl_creaFecha, facpl_modifUsr, facpl_modifFecha FROM ap_factura_linea WHERE 1 ".$where;
        $result = mysql_query($sql);
      $num_rows = mysql_num_rows($result);
      $a_tmp = array();
      if($num_rows>0){
        while ($o_valor = mysql_fetch_object($result)) {
          $a_tmp[] = $this->insertObjectBN($o_valor);
        }
      }
      
      return count($a_tmp)>0?$a_tmp:null;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  public function selectNumRows()
  {
    try
    {
      $where = $this->buildConsult();

      $o_conexion = new BaseDatos();
      $con = $o_conexion->conectar();
      $sql = "SELECT facpl_facId, facpl_lineaId, facpl_lineaTipo, facpl_artId, facpl_artNombre, facpl_impuestoAfecto_sw, facpl_cant, facpl_UoM, facpl_precio, facpl_precio_mn, facpl_impuesto, facpl_dcto, facpl_subtotal, facpl_subtotalNeto, facpl_obs, facpl_creaUsr, facpl_creaFecha, facpl_modifUsr, facpl_modifFecha FROM ap_factura_linea WHERE 1 ".$where;
        $result = mysql_query($sql);
      $num_rows = mysql_num_rows($result);
      
      return $num_rows;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  public function buildConsult()
  {
    try
    {
      $where = "";

      if($this->getFacId()){ $where .= " AND facpl_facid = '".$this->getFacId()."'"; }
      if($this->getLineaId()){ $where .= " AND facpl_lineaid = '".$this->getLineaId()."'"; }
      if($this->getLineaTipo()){ $where .= " AND facpl_lineatipo = '".$this->getLineaTipo()."'"; }
      if($this->getArtId()){ $where .= " AND facpl_artid = '".$this->getArtId()."'"; }
      if($this->getArtNombre()){ $where .= " AND facpl_artnombre = '".$this->getArtNombre()."'"; }
      if($this->getImpuestoAfectoSw()){ $where .= " AND facpl_impuestoafecto_sw = '".$this->getImpuestoAfectoSw()."'"; }
      if($this->getCant()){ $where .= " AND facpl_cant = '".$this->getCant()."'"; }
      if($this->getUoM()){ $where .= " AND facpl_uom = '".$this->getUoM()."'"; }
      if($this->getPrecio()){ $where .= " AND facpl_precio = '".$this->getPrecio()."'"; }
      if($this->getPrecioMn()){ $where .= " AND facpl_precio_mn = '".$this->getPrecioMn()."'"; }
      if($this->getImpuesto()){ $where .= " AND facpl_impuesto = '".$this->getImpuesto()."'"; }
      if($this->getDcto()){ $where .= " AND facpl_dcto = '".$this->getDcto()."'"; }
      if($this->getSubtotal()){ $where .= " AND facpl_subtotal = '".$this->getSubtotal()."'"; }
      if($this->getSubtotalNeto()){ $where .= " AND facpl_subtotalneto = '".$this->getSubtotalNeto()."'"; }
      if($this->getObs()){ $where .= " AND facpl_obs = '".$this->getObs()."'"; }
      if($this->getCreaUsr()){ $where .= " AND facpl_creausr = '".$this->getCreaUsr()."'"; }
      if($this->getCreaFecha()){ $where .= " AND facpl_creafecha = '".$this->getCreaFecha()."'"; }
      if($this->getModifUsr()){ $where .= " AND facpl_modifusr = '".$this->getModifUsr()."'"; }
      if($this->getModifFecha()){ $where .= " AND facpl_modiffecha = '".$this->getModifFecha()."'"; }
      return $where;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  public function buildOrderByField()
  {
    try
    {
      $order_by = "";

      if(property_exists($this,'sortCampos')){
      if($this->sortCampos != ""){
        $a_valCampos = explode(";", $this->sortCampos);
        $a_valtype   = explode(";", $this->sortType);
        $order_by = " ORDER BY ";
        $a_order = array();
        foreach ($a_valCampos as $key => $val){
          $sTmpOrder = "";
          if(array_key_exists($val, self::$fieldNames)){
            $sTmpOrder .= self::$fieldNames[$val];
            switch ($a_valtype[$key]){
              case 1: $sTmpOrder .= " ASC"; break;
              case 2: $sTmpOrder .= " DESC"; break;
            }
            $a_order[] = $sTmpOrder;
          }
        }
        $order_by .= implode(", ", $a_order);
      }
      }
      return $order_by;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  /* B�squeda del registro por la llave primaria */
  public function selectByPK()
  {
    try
    {
      $o_conexion = new BaseDatos();
      $con = $o_conexion->conectar();
      $sql = "SELECT facpl_facId, facpl_lineaId, facpl_lineaTipo, facpl_artId, facpl_artNombre, facpl_impuestoAfecto_sw, facpl_cant, facpl_UoM, facpl_precio, facpl_precio_mn, facpl_impuesto, facpl_dcto, facpl_subtotal, facpl_subtotalNeto, facpl_obs, facpl_creaUsr, facpl_creaFecha, facpl_modifUsr, facpl_modifFecha FROM ap_factura_linea WHERE 1 AND facpl_facId='".$this->getFacId()."' AND facpl_lineaId='".$this->getLineaId()."'; ";
        $result = mysql_query($sql);
      $o_Propel = $result ? mysql_fetch_object($result) : null;
      

      return $o_Propel?$this->insertObjectBN($o_Propel):null;

    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  /* Devuelve un objeto con m�todos de acceso */
  private function insertObjectBN($o_propel){
    $o_BaseApFacturaLineaFN = new ApFacturaLineaBN();
    $o_BaseApFacturaLineaFN->setFacId($o_propel->facpl_facId);
    $o_BaseApFacturaLineaFN->setLineaId($o_propel->facpl_lineaId);
    $o_BaseApFacturaLineaFN->setLineaTipo($o_propel->facpl_lineaTipo);
    $o_BaseApFacturaLineaFN->setArtId($o_propel->facpl_artId);
    $o_BaseApFacturaLineaFN->setArtNombre($o_propel->facpl_artNombre);
    $o_BaseApFacturaLineaFN->setImpuestoAfectoSw($o_propel->facpl_impuestoAfecto_sw);
    $o_BaseApFacturaLineaFN->setCant($o_propel->facpl_cant);
    $o_BaseApFacturaLineaFN->setUoM($o_propel->facpl_UoM);
    $o_BaseApFacturaLineaFN->setPrecio($o_propel->facpl_precio);
    $o_BaseApFacturaLineaFN->setPrecioMn($o_propel->facpl_precio_mn);
    $o_BaseApFacturaLineaFN->setImpuesto($o_propel->facpl_impuesto);
    $o_BaseApFacturaLineaFN->setDcto($o_propel->facpl_dcto);
    $o_BaseApFacturaLineaFN->setSubtotal($o_propel->facpl_subtotal);
    $o_BaseApFacturaLineaFN->setSubtotalNeto($o_propel->facpl_subtotalNeto);
    $o_BaseApFacturaLineaFN->setObs($o_propel->facpl_obs);
    $o_BaseApFacturaLineaFN->setCreaUsr($o_propel->facpl_creaUsr);
    $o_BaseApFacturaLineaFN->setCreaFecha($o_propel->facpl_creaFecha);
    $o_BaseApFacturaLineaFN->setModifUsr($o_propel->facpl_modifUsr);
    $o_BaseApFacturaLineaFN->setModifFecha($o_propel->facpl_modifFecha);
    return $o_BaseApFacturaLineaFN;
  }
}